/* © 2007 Adobe Systems Incorporated. All rights reserved. */
package com.shubhjai.musiclib.commands
{
	import com.shubhjai.musiclib.IncludeClasses;
	import com.shubhjai.musiclib.data.InitDatabaseCommand;
	import com.shubhjai.musiclib.events.CommandCompleteEvent;
	import com.shubhjai.musiclib.events.CommandProgressEvent;
	import com.shubhjai.musiclib.managers.ConfigManager;
	import com.shubhjai.musiclib.managers.WindowPositionManager;
	
	import flash.display.Stage;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	
	/**
	 * This command is used to manage initialization of the application.
	 */ 
	public class InitApplicationCommand extends Command
	{
		private static var CONFIG_URL : String = "config/tamilmusiclibrary_config.xml";
		
		private var configManager : ConfigManager;
		
		private var stage : Stage;
		
		/**
		 * Constructor.
		 * @param The stage. Required for several
		 */ 
		public function InitApplicationCommand( stage:Stage ) : void
		{
			super();
			this.stage = stage;
			
			/** Used to include parser classes that may be specified in configuration files. **/
			var ic : IncludeClasses;
				
			WindowPositionManager.getInstance().init( stage.window );
		}
		
		/**
		 * Execute the application initialization command.
		 */ 
		override public function execute() : void
		{
			configManager = ConfigManager.getInstance();
			
			configManager.addEventListener( Event.COMPLETE, onConfigLoad );
			configManager.addEventListener( ErrorEvent.ERROR, onError );
			configManager.loadConfig( CONFIG_URL );
			
			progress = 5;
			notifyProgress( 5, "Initializing application ..." );
		}
		
		private function onConfigLoad( evt:Event ) : void
		{
			configManager.removeEventListener( Event.COMPLETE, onConfigLoad );
			configManager.removeEventListener( ErrorEvent.ERROR, onError );
			
			/* NOTE - In a production use, you could use somoething similar to the following
			command to manage application updates. 
			*/
			
			var cmd : UpdateApplicationCommand = new UpdateApplicationCommand();
			cmd.addEventListener( CommandCompleteEvent.COMPLETE, onUpdateAppComplete );
			cmd.addEventListener( CommandProgressEvent.PROGRESS, onUpdateAppProgress );
			cmd.addEventListener( ErrorEvent.ERROR, onError );
			cmd.execute();
			
			
			initDatabase();
		}
		
		private function onUpdateAppProgress( evt:CommandProgressEvent ) : void
		{
			notifyProgress( 30, evt.progressMessage );
		}
		
		private function onUpdateAppComplete( evt:Event ) : void
		{	
			var cmd : Command = evt.target as Command;
			cmd.removeEventListener( CommandCompleteEvent.COMPLETE, onUpdateAppComplete );
			cmd.removeEventListener( CommandProgressEvent.PROGRESS, onUpdateAppProgress );
			cmd.removeEventListener( ErrorEvent.ERROR, onError );
			
			initDatabase();
		}
		
		private function initDatabase() : void
		{
			var cmd1 : InitDatabaseCommand = new InitDatabaseCommand();
			cmd1.addEventListener( CommandCompleteEvent.COMPLETE, onInitDBComplete );
			cmd1.addEventListener( ErrorEvent.ERROR, onError );
			cmd1.execute();
		}
		
		
		private function onInitDBComplete( evt:Event ) : void
		{
			var cmd : InitDatabaseCommand = evt.target as InitDatabaseCommand;
			cmd.removeEventListener( CommandCompleteEvent.COMPLETE, onInitDBComplete );
			cmd.removeEventListener( ErrorEvent.ERROR, onError );
			
			notifyComplete();
		}
		
		private function onError( evt:ErrorEvent ) : void
		{
			var cmd : Command = evt.target as Command;
			cmd.removeEventListener( CommandCompleteEvent.COMPLETE, onInitDBComplete );
			cmd.removeEventListener( ErrorEvent.ERROR, onError );
			
			notifyError( evt.text );
		}
	}
}